#pragma once
#include <QQuickPaintedItem>
#include <QImage>
#include <QPainter>
#include <QQuickView>

class ImageWrapper : public QObject
{
    Q_OBJECT
public:
    ~ImageWrapper() {
    }

    void setImage(const QImage& image) { m_image = image; };
    const QImage& image() { return m_image; };
private:
    QImage m_image;
};
Q_DECLARE_METATYPE(ImageWrapper*)

class ImageProvide : public QQuickPaintedItem
{
    Q_OBJECT
public:
    explicit ImageProvide(QQuickItem *parent = nullptr);

    Q_INVOKABLE void initImage( QString path );
    Q_INVOKABLE void updateImage();
    Q_INVOKABLE QPointF imagePosition();
    Q_INVOKABLE int imageWidth() { return m_imageWidth; };
    Q_INVOKABLE int imageHeight(){ return m_imageHeight; };
    Q_INVOKABLE ImageWrapper* image() { imageWrapper.setImage(m_imageThumb); return &imageWrapper; }
    Q_INVOKABLE bool isNullImage() { return m_imageThumb.isNull(); }
    Q_INVOKABLE void setNewSize(int width, int height) { m_imageWidth = width; m_imageHeight = height; }
    Q_INVOKABLE void setCurPosition(qreal zoomFactor, qreal x, qreal y);
    Q_INVOKABLE void setCurOutPosition(qreal zoomFactor, qreal x, qreal y);
    Q_INVOKABLE void movePosition(qreal x, qreal y);
    Q_INVOKABLE void leftRightMirrored();
    Q_INVOKABLE void upDownMirrored();
    Q_INVOKABLE void leftRotation();
    Q_INVOKABLE void rightRotation();

signals:
    void signalImageWidth();
    void signalImageHeight();

protected:
    void paint(QPainter *painter) override;

private:
    QImage m_imageThumb;
    int m_imageWidth = 1.0;
    int m_imageHeight = 1.0;
    QRectF m_clipRect;
    // 包装一层机器信息给qml使用
    ImageWrapper imageWrapper;
};

Q_DECLARE_METATYPE(QImage*)
